package ch.zbw.sysventory.server.control;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import ch.zbw.sysventory.server.service.ComputerScanResultService;
import ch.zbw.sysventory.server.service.ComputerScanService;

@Component
@Controller
public class StatisticController {

	@Autowired
	private ComputerScanService computerScanService;

	@Autowired
	private ComputerScanResultService computerScanResultService;

	@RequestMapping(value = "/scanstatistic", method = RequestMethod.GET)
	public ModelAndView showScanStatistic(Model model) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
		model.addAttribute("startDate", simpleDateFormat.format(calendar.getTime()));
		model.addAttribute("endDate", simpleDateFormat.format(new Date()));
		model.addAttribute("amountComputerScans", this.computerScanService.countComputerScansBetween(calendar.getTime(), new Date()));
		model.addAttribute("amountComputersScanned", this.computerScanService.countComputersScannedBetween(calendar.getTime(), new Date()));
		model.addAttribute("result_list",
				this.computerScanResultService.listLatestScanResultsByComputers(this.computerScanService.getComputersScannedBetween(calendar.getTime(), new Date())));
		return new ModelAndView("scanstatistic");
	}

	@RequestMapping(value = "/scanstatistic", method = RequestMethod.POST)
	public ModelAndView refreshScanStatistic(Model model, @ModelAttribute("startDate") String startDateString, @ModelAttribute("endDate") String endDateString) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
		Date startDate = null;
		Date endDate = null;
		if (startDateString != null && !"".equals(startDateString)) {
			try {
				startDate = simpleDateFormat.parse(startDateString);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (endDateString != null && !"".equals(endDateString)) {
			try {
				endDate = simpleDateFormat.parse(endDateString);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (startDate != null && endDate != null) {
			model.addAttribute("amountComputerScans", this.computerScanService.countComputerScansBetween(startDate, endDate));
			model.addAttribute("amountComputersScanned", this.computerScanService.countComputersScannedBetween(startDate, endDate));
			model.addAttribute("result_list",
					this.computerScanResultService.listLatestScanResultsByComputers(this.computerScanService.getComputersScannedBetween(startDate, endDate)));
		}

		return new ModelAndView("scanstatistic");
	}

}
